class ShopCar {
    // 加减删按钮，每个商品对应的数量，总数量，每个商品的小计，总价
    constructor(obtn, onum, oaddnum, ototal, oaddprice) {
        this.obtn = obtn;
        this.onum = onum;
        this.oaddnum = oaddnum;
        this.ototal = ototal;
        this.oaddprice = oaddprice;
        this.end();
    }
    //更新总数量和总价格
    add() {
        let n = 0;
        for (let i = 0; i < this.onum.length; i++) {
            n += this.onum[i].innerHTML / 1;
        }
        this.oaddnum.innerHTML = n;
        let price = 0;
        for (let i = 0; i < this.ototal.length; i++) {
            price += this.ototal[i].innerHTML / 1;
        }
        this.oaddprice.innerHTML = price;

    }
    //+按钮
    up(btn) {
        //加数字
        btn.previousElementSibling.firstElementChild.innerHTML = btn.previousElementSibling.firstElementChild.innerHTML / 1 + 1;
        //加小计
        btn.parentNode.previousElementSibling.innerHTML =
            btn.parentNode.parentNode.previousElementSibling.firstElementChild.nextElementSibling.innerHTML * btn.previousElementSibling.firstElementChild.innerHTML;
        //更新总数和总价
        $.post(
            "./php/updateGoodsCount.php",
            {
                "goodsCount": btn.previousElementSibling.firstElementChild.innerHTML,
                "vipName": getCookie("vipName"),
                "goodsId": btn.getAttribute('goodsId')
            },
            function (data) {
                console.log(data + "加")
            }
        );
        this.add()
    }
    //-按钮
    down(btn) {
        //减数字
        let n = btn.nextElementSibling.firstElementChild.innerHTML
        if (n > 1) {
            btn.nextElementSibling.firstElementChild.innerHTML = n / 1 - 1;
            //加小计
            btn.parentNode.previousElementSibling.innerHTML =
                btn.parentNode.parentNode.previousElementSibling.firstElementChild.nextElementSibling.innerHTML * btn.nextElementSibling.firstElementChild.innerHTML;
            $.post(
                "./php/updateGoodsCount.php",
                {
                    "goodsCount": btn.nextElementSibling.firstElementChild.innerHTML,
                    "vipName": getCookie("vipName"),
                    "goodsId": btn.getAttribute('goodsId')
                },
                function (data) {
                    console.log(data + "减")
                }
            );
        }
        this.add();
    }
    //删除按钮
    del(btn) {
        let tr = btn.parentNode.parentNode.parentNode;
        $.post(
            "./php/deleteGoods.php",
            {
                "vipName": getCookie("vipName"),
                "goodsId": btn.getAttribute('goodsId')
            },
            function (data) {
                console.log(data + "删")
            }
        );
        tr.remove();
        this.add()
    }
    //总函数
    end() {
        let that = this;
        this.add();
        for (let i = 0; i < that.obtn.length; i++) {
            if ((i + 1) % 3 == 1) {
                this.obtn[i].onclick = function () {
                    that.down(this)
                }
            } else if ((i + 1) % 3 == 2) {
                this.obtn[i].onclick = function () {
                    that.up(this)
                }
            } else if ((i + 1) % 3 == 0) {
                this.obtn[i].onclick = function () {
                    that.del(this);
                }
            }
        }
    }
}